package com.qk.common;

import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Author: zhangchuang
 * @Date: 2025/09/29 09:24
 * @Description: 返回结果
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Result {

    private static final Integer SUCCESS_CODE = 1;
    private static final String SUCCESS_MESSAGE = "success";
    private static final Integer ERROR_CODE = 0;
    private static final String ERROR_MESSAGE = "error";
    private static final String DEFAULT_ERROR_MESSAGE = "系统异常";

    private Integer code; //编码：1成功，0为失败
    private String msg; //错误信息
    private Object data; //数据

    public static Result success() {
        return Result.builder()
                .code(SUCCESS_CODE)
                .msg(SUCCESS_MESSAGE)
                .build();
    }

    public static Result success(Object object) {
        return Result.builder()
                .data(object)
                .code(SUCCESS_CODE)
                .msg(SUCCESS_MESSAGE)
                .build();
    }

    public static Result error(String msg) {
        return Result.builder()
                .msg(StrUtil.isEmpty(msg) ? ERROR_MESSAGE : msg)
                .code(ERROR_CODE)
                .build();
    }

    public static Result error() {
        return Result.builder()
                .msg(DEFAULT_ERROR_MESSAGE)
                .code(ERROR_CODE)
                .build();
    }

}
